package com.nowcoder.community.controller.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class TestInterceptor implements HandlerInterceptor {
    /**
     * @Author hhwu
     * @Description //TODO 声明一个拦截器，以便在配置类中配置添加
     * @Date 11:34 2022/5/20
     **/
    private static final Logger logger = LoggerFactory.getLogger(TestInterceptor.class);

    // 在controller之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * @Author hhwu
         * @Description //TODO 返回false就不会继续执行controller了
         * @Date 11:21 2022/5/20
         * @Param [request, response, handler]
         * @return boolean
         **/
        logger.debug("preHandle:" + handler.toString());
        return true;
    }

    // 在controller之后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.debug("postHandle:" + handler.toString());
    }

    // 在程序的最后执行，也就是在模版引擎之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.debug("afterCompletion:" + handler.toString());
    }
}
